package com.changanw.code20;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Stack;

/**
 * @author changanw
 * @description
 * @date 2023/7/8 15:03
 */
public class Code20 {
  public boolean isValid(String s){
    //如果为基数直接返回false
    int n = s.length();
    if (n % 2 == 1) {
      return false;
    }

    Map<Character,Character> pairs  = new HashMap<>();
    pairs .put(')', '(');
    pairs .put('}', '{');
    pairs .put(']', '[');
    Deque<Character> stack = new LinkedList<Character>();
    for (int i = 0; i < n; i++) {
      char ch = s.charAt(i);
      if (pairs.containsKey(ch)) {
        if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
          return false;
        }
        stack.pop();
      } else {
        stack.push(ch);
      }
    }
    return stack.isEmpty();
  }

  public static void main(String[] args) {
    /*Code20 code20 = new Code20();
    String s = "{}()";
    boolean valid = code20.isValid(s);
    System.out.println(valid);*/
    Stack<Integer> stack = new Stack<Integer>();
    stack.add(10);
    stack.add(20);
    stack.add(30);
    stack.add(40);
    stack.add(50);
    while (!stack.isEmpty()){
      System.out.println(stack.pop());
    }
  }
}
